<?php

namespace App\Http\Controllers;


use App\Http\Models\Post;
use App\Http\Models\Zan;
use Illuminate\Http\Request;

class PostController extends Controller
{

    public function isLogin (  )
    {
        $user_id = \Auth::id();

        if(!$user_id){
//dd(1);
            return false;
        }
        return true;
    }

    public function index (  )
    {
//        dd(Post::with('comments')->find(33));

        $posts = Post::withCount('comments')->withCount('zans')->orderBy('updated_at','desc')->paginate(5);

        return view('post.index',compact('posts'));
    }



    public function detail ( $id )
    {
        $detail = Post::with(['comments'=>function($query){
            return $query->orderBy('updated_at','desc');
        }])->find($id);
//        判断是否为该用户的文章
        $user = \Auth::user();
//        dd($user->can('update', $detail));

        $isZan = false;
        if($user){
            if(Zan::where([['user_id',$user['id']],['post_id',$id]])->first()){
                $isZan = true;
            }
        }

        return view('post.show',compact('detail','isZan'));
    }

    public function create (  )
    {

        if($this->isLogin()){
            return view('post.create');
        }else{
            return redirect('/login/index');
        }
    }

    public function createSubmit (Request $request )
    {

//        登录才能写文章

        $user_id = \Auth::id();
        if(!$user_id){
            return redirect('/login/index');
        }


        $this->validate($request, [
            'title' => 'required|max:255|min:4',
            'content' => 'required|min:10',
        ]);

        if(Post::create(array_merge(request(['title','content']),compact('user_id')))){
            return redirect('/post/index');
        }


//        第二种错误提示方案
//        return redirect('/post/create')->with('message', '失败！代号不能有中文');

//        dd(request(['title','content']));

    }

    public function edit ( $id )
    {

        $post = Post::find($id);

        return view('post.edit',compact('post'));
    }


    public function editSubmit ( $id, Request $request )
    {
        $this->validate($request, [
            'title' => 'required|max:255|min:4',
            'content' => 'required|min:10',
        ]);

        $post = Post::find($id);
//        $title =1;
        $this->authorize('update', $post);

        $user_id = \Auth::id();
        $param = array_merge(request(['title', 'content']),compact('id','user_id'));
        $rs = Post::create($param);

        if($rs){
            return redirect('/post/index');
        }

    }


    public function zan ( Request $request )
    {
        if(!$this->isLogin()){
            return redirect('/login/index');
        }

        $this->validate($request, [
            'post_id' => 'required|int',
        ]);

        $user_id = \Auth::id();

        if(Zan::where([['user_id',$user_id],['post_id',$request['post_id']]])->first()){

            Zan::where([['user_id',$user_id],['post_id',$request['post_id']]])->delete();

        }else{

            $rs = Zan::create(array_merge(request(['post_id']),compact('user_id')));
        }


        return redirect('/post/detail/'.$request['post_id']);

    }


    /**
     * 使用wangEditor小结，插件比较好用，能较好的实现富文本功能
     * 缺点：用户删除的图片无法再服务器上删除
     * @param Request $request
     * @return string
     */
    public function uploadImage ( Request $request )
    {
        $image = $request->file('wangEditorH5File');

        return asset('uploads/'.$image->storeAs('xxx',date('Y-m-d-H-i-s'.time()),'uploads'));
    }
}
